package com.yuhong.reggie.service.impl;

import com.yuhong.reggie.entity.User;
import com.yuhong.reggie.exception.BusinessException;
import com.yuhong.reggie.mapper.UserMapper;
import com.yuhong.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public User login(String phone, String userCode, String sessionCode) {
        //校验验证码是否正确,如果不正确返回错误信息
        if (!userCode.equals(sessionCode)){
            throw new BusinessException("验证码错误");
        }
        User loginUser = userMapper.findByPhone(phone);
        if(loginUser!=null){
            if(loginUser.getStatus()==1){
                throw new BusinessException("用户已被禁用");
            }
        }else {
            loginUser = new User();
            loginUser.setStatus(0);
            loginUser.setPhone(phone);
            userMapper.save(loginUser);
        }
        return loginUser;
    }
}
